package com.flyme.common.register.config;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
import org.springframework.beans.factory.parsing.CompositeComponentDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;

/**
 *
 */
public class AnnotationDrivenBeanDefinitionParser implements BeanDefinitionParser {

    private static final String EXPORT_SERVICE_BEAN_PROCESSOR_NAME = "com.uc.common.service.handler.internalExportServiceAnnotationBeanPostProcessor";

    @Override
    public BeanDefinition parse(Element element, ParserContext parserContext) {

        Object source = parserContext.extractSource(element);
        // Register component for the surrounding <task:annotation-driven> element.
        CompositeComponentDefinition compDefinition = new CompositeComponentDefinition(element.getTagName(), source);
        parserContext.pushContainingComponent(compDefinition);

        // Nest the concrete post-processor bean in the surrounding component.
        BeanDefinitionRegistry registry = parserContext.getRegistry();

        if (registry.containsBeanDefinition(EXPORT_SERVICE_BEAN_PROCESSOR_NAME)) {
            parserContext.getReaderContext().error("Only one ExportServiceAnnotationBeanPostProcessor may exist within the context.", source);
        } else {
            BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition("com.uc.common.service.handler.ExportServiceAnnotationBeanPostProcessor");
            builder.getRawBeanDefinition().setSource(source);
            String isOpen = element.getAttribute("switch");
            if (StringUtils.hasText(isOpen)) {
                builder.addPropertyValue("processSwitch", Boolean.valueOf(isOpen));
            }
            registerPostProcessor(parserContext, builder, EXPORT_SERVICE_BEAN_PROCESSOR_NAME);
        }
        // Finally register the composite component.
        parserContext.popAndRegisterContainingComponent();
        return null;
    }

    private static void registerPostProcessor(ParserContext parserContext, BeanDefinitionBuilder builder, String beanName) {
        builder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
        parserContext.getRegistry().registerBeanDefinition(beanName, builder.getBeanDefinition());
        BeanDefinitionHolder holder = new BeanDefinitionHolder(builder.getBeanDefinition(), beanName);
        parserContext.registerComponent(new BeanComponentDefinition(holder));
    }

}
